#!/usr/bin/env python
"""read and write cpudata to /sys"""
def GetCpuData():
	"""cpudata.keys == ['trans_table', 'scaling_max_freq', 'scaling_governor', 'scaling_min_freq', 'scaling_available_governors', 'scaling_available_frequencies', 'time_in_state', 'total_trans']"""
	cpudatadir = '/sys/devices/system/cpu/cpu0/cpufreq/'
	data = {'scaling_available_frequencies':'','scaling_available_governors':'','scaling_governor':'','scaling_max_freq':'','scaling_min_freq':'','time_in_state':'','total_trans':'','trans_table':''}
	for i in data.keys():
		if i not in ['time_in_state','total_trans','trans_table']:
			d = open(cpudatadir+i).read()
			data[i] = d
		else: 
			d = open(cpudatadir+'stats/'+i).read()
			data[i] = d
	return data
	
def SetCpuData(scaling_max_freq=0,scaling_min_freq=0,scaling_governor=0):
	cpudatadir = '/sys/devices/system/cpu/cpu0/cpufreq/'
	targetlist = ['scaling_min_freq','scaling_max_freq','scaling_governor']
	targetlist = [cpudatadir + i for i in targetlist]
	changelist = [scaling_min_freq,scaling_max_freq,scaling_governor]
	for i in range(3):
		if not changelist[i] == 0:
			d = open(targetlist[i],'w')
			d.write(str(changelist[i]))
			d.close()
		else : pass
	
			
	
		
	
def Fandata(mode,state='None'):
	"""when mode it 'set',state instruct what cpu cooling mode should be use,0 mean active,1 mean passive"""
	dir = '/proc/acpi/thermal_zone/THRM/'
	if mode == 'get':
		d = open(dir+'cooling_mode','r')
		D = d.read()
		d.close()
		t = open(dir+'temperature','r')
		T = t.read()
		T = T.replace('   ','')
		t.close()
		return D , T
	if mode == 'set':
		d = open(dir+'cooling_mode','w')
		d.write(state)
		d.close()
	